package com.zdft.promotion.common.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ImportFileUtils {

    /**
     * @param list 失败文件list
     * @param t 失败文件类
     * @param pathPrix  Excel上传路径
     * @param fileName 文件名称
     * @return 保存路径
     */
    public static String getFileSaveExcel(List<?> list,Class<?> t,String pathPrix,String fileName){
        ExportParams exportParams = new ExportParams();
        exportParams.setType(ExcelType.XSSF);
        Workbook errorWorkBook = ExcelExportUtil.exportExcel(exportParams, t, list);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        String pathDir = sdf.format(now);
        String filePath = pathPrix + File.separator + pathDir + File.separator;
        File dir = new File(filePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        String errFilePath = filePath + fileName + now.getTime() + ".xlsx";
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(errFilePath);
            errorWorkBook.write(fileOutputStream);
            fileOutputStream.close();
        } catch (Exception e) {

            e.printStackTrace();
        }
        return errFilePath;
    }
}
